package com.autonavi.yunda.yunji.core.engine.script;

import com.autonavi.yunda.yunji.core.engine.javascript.JSFunctionContext;
import com.autonavi.yunda.yunji.core.engine.javascript.JsExecutor;
import com.autonavi.yunda.yunji.core.enums.FunctionType;
import com.autonavi.yunda.yunji.core.vo.node.JsFunctionNode;
import com.autonavi.yunda.yunji.core.vo.param.NodeParam;
import lombok.Data;

import java.sql.Timestamp;
import java.util.Map;
import java.util.stream.Collectors;

import static com.autonavi.yunda.yunji.core.engine.javascript.JsExecutor.parserContext;

/**
 * @author cizhong.wcs
 * @date 2022/7/9 08:30
 */
@Data
public class JsFunctionScript implements IFunctionScript {
    private JSFunctionContext context;
    private Timestamp updateTime;

    public JsFunctionScript(JsFunctionNode node) {
        this.context = parserContext(node.script, node.params.stream().map(NodeParam::getParamNameInScript).collect(Collectors.toSet()));
    }

    @Override
    public Object exeFun(Map<String, Object> params, String methodName, Object... args) {
        return JsExecutor.doExec(context, params);
    }

    @Override
    public String getScript() {
        return context.getScript();
    }

    @Override
    public FunctionType getFunctionType() {
        return FunctionType.javascript;
    }
}
